package com.ruoyi.web.controller.house;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.data.DataUtils;
import com.ruoyi.door.domain.HouseDoorT;
import com.ruoyi.door.service.IHouseDoorTService;
import com.ruoyi.info.domain.HouseDoorInfoT;
import com.ruoyi.noDoor.domain.HouseNoDoorT;
import com.ruoyi.noDoor.service.IHouseNoDoorTService;
import com.ruoyi.purpose.domain.HousePurposeT;
import com.ruoyi.purpose.service.IHousePurposeTService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 户信息修改Controller
 * 
 * @author tyq
 * @date 2023-04-24
 */
@RestController
@RequestMapping("/house/noDoor")
public class HouseNoDoorQueryController extends BaseController
{
    @Autowired
    private IHouseNoDoorTService houseNoDoorTService;

    @Autowired
    private IHouseDoorTService houseDoorTService;

    @Autowired
    private IHousePurposeTService housePurposeTService;

    /**
     * 查询户信息修改列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseNoDoorT houseNoDoorT)
    {
        startPage();
        List<HouseNoDoorT> list = houseNoDoorTService.selectHouseNoDoorTList(houseNoDoorT);
        return getDataTable(list);
    }


    /**
     * 新增户管理
     */
    @Log(title = "户管理", businessType = BusinessType.INSERT)
    @PostMapping("/addNoDoor")
    public AjaxResult add(@RequestBody HouseNoDoorT houseNoDoorT)
    {
        return toAjax(houseNoDoorTService.insertNoDoorInfo(houseNoDoorT));
    }

    /**
     * 新增户管理
     */
    @Log(title = "户管理", businessType = BusinessType.INSERT)
    @PostMapping("/addDoors")
    public AjaxResult addDoors(@RequestBody HouseNoDoorT houseNoDoorT)
    {
        Integer floorNumber = houseNoDoorT.getFloorNumber();
        Integer result = 0;
        HouseNoDoorT houseNoDoorTTemp = new HouseNoDoorT();
        HousePurposeT purposeVO = new HousePurposeT();
        for(int floor = 1 ; floor <= floorNumber ; floor ++) {
            String doorName = floor + houseNoDoorT.getDoorNumber();
            int id = houseDoorTService.getDoorId();
            houseNoDoorT.setDoorName(doorName);
            houseNoDoorTTemp = houseNoDoorTService.getDoorDetailTemp(houseNoDoorT);
            if(houseNoDoorTTemp == null) {
                purposeVO = housePurposeTService.selectHousePurposeTByPurposeId(houseNoDoorT.getPurposeId());
                houseNoDoorT.setBudgetMoney(DataUtils.doubleCeil(DataUtils.multiplyMoney(houseNoDoorT.getSpace(),purposeVO.getPurposeMoney(),purposeVO.getBuildMoney())));
                houseNoDoorT.setBalance(0d);
                houseNoDoorT.setCreater(getUsername());
                String houseId = (DateUtils.dateTimeNow()).substring(2,4) + String.format("%" + 5 + "d", id+1).replace(" ", "0");
                houseNoDoorT.setHouseId(houseId);
                houseNoDoorT.setRateType("0");
                result = houseNoDoorTService.insertHouseDoorT(houseNoDoorT);
            }
        }
        return toAjax(houseNoDoorTService.insertNoDoorInfo(houseNoDoorT));
    }

    /**
     * 导出户管理列表
     */
    @Log(title = "户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseNoDoorT houseNoDoorT)
    {
        List<HouseNoDoorT> list = houseNoDoorTService.selectHouseNoDoorTList(houseNoDoorT);
        ExcelUtil<HouseNoDoorT> util = new ExcelUtil<HouseNoDoorT>(HouseNoDoorT.class);
        util.exportExcel(response, list, "未建户数据");
    }



}
